package com.thti.mecool.client.service;

import android.app.Service;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.os.Handler;
import android.os.IBinder;

import com.baidu.location.BDLocation;
import com.baidu.location.BDLocationListener;
import com.baidu.location.LocationClient;
import com.baidu.location.LocationClientOption;
import com.thti.mecool.client.command.send.location.LocationSend;

public class LocateService extends Service {

	private LocationClient mLocationClient = null;
	private int Spacetime = 30*60000; //30分钟
	private int i = 0;
	private boolean SaveTask;
	private String UserName;
	private SharedPreferences save_LoginInfo ;
	private Editor editor;
	private Runnable runnable = new Runnable() {
		@Override
		public void run() {
			mLocationClient.requestLocation();
			handler.postDelayed(runnable, Spacetime);
		}
	};

	private Handler handler = new Handler() {
		@Override
		public void handleMessage(android.os.Message msg) {
			switch (msg.what) {
			case 1:
				post(runnable);
				break;
			}
		}
	};
	private BDLocationListener bd = new BDLocationListener() {
		String lat,lon;
		@Override
		public void onReceiveLocation(BDLocation location) {
			if (location == null) {
				return;
			}
			if (location.getLocType() == 65||location.getLocType() == 61||location.getLocType() == 161) {
				lat=String.valueOf(location.getLatitude());
				lon=String.valueOf(location.getLongitude());
			}else{
				lat=null;
				lon=null;
			}
			if (location.getAddrStr() != null) {
				if(!SaveTask){
					if(UserName!=null){
				LocationSend.send(UserName,lat!=null?Double.valueOf(lat):null, lon!=null?Double.valueOf(lon):null,location.getTime());}}
				else{
					SaveTask=false;
				}
				editor.putString("Latitude",lat);
				editor.putString("Longitude",lon);
				editor.commit();
			} else {
				editor.putString("Latitude",lat);
				editor.putString("Longitude",lon);
				editor.commit();
				i++;
				if (i == 10){
					if(!SaveTask){
						if(UserName!=null){
					LocationSend.send(UserName,lat!=null?Double.valueOf(lat):null, lon!=null?Double.valueOf(lon):null, location.getTime());}
					}
					else{
						SaveTask=false;
					}
					editor.putString("Latitude",lat);
					editor.putString("Longitude",lon);
					editor.putString("LocalChangeTime",location.getTime());
					editor.commit();
					i=0;
				}else {
					Thread t = new Thread() {
						@Override
						public void run() {
							try {
								sleep(1000);
							} catch (InterruptedException e) {
							}
							handler.post(runnable);
						};
					};
					t.start(); 
				}
			}
		}
		@Override
		public void onReceivePoi(BDLocation arg0) {
			// TODO Auto-generated method stub
			
		}
	};

	@Override
	public IBinder onBind(Intent intent) {
		return null;
	}
	
	@Override
	public void onCreate() {
		super.onCreate();
		System.out.println("LocationService OnCreate!");
		mLocationClient = new LocationClient(this);
		LocationClientOption option = new LocationClientOption();
		option.setOpenGps(true);
		option.setCoorType("bd09ll");
		option.setPriority(LocationClientOption.NetWorkFirst);
		// option.setProdName("MecoolLocation");
		option.setScanSpan(Spacetime);
		mLocationClient.setLocOption(option);
		mLocationClient.registerLocationListener(bd);
	}

	@Override
	public void onStart(Intent intent, int startId) {
		super.onStart(intent, startId);
		int flag=-1;
		if(intent!=null){
		flag=intent.getIntExtra("flag", -1);
		if(flag==101){
			SaveTask=true;
		}else{
			SaveTask=false;
		}
		}else{
			SaveTask=false;
		}
		save_LoginInfo = getSharedPreferences("LocationService", 0);
		UserName=save_LoginInfo.getString("UserLoginName", null);
		editor=save_LoginInfo.edit();
		editor.remove("Latitude").remove("Longitude");
		handler.sendEmptyMessage(1);
		mLocationClient.start();
	}

	@Override
	public void onDestroy() {
		if (mLocationClient != null && mLocationClient.isStarted()) {
			mLocationClient.stop();
			mLocationClient = null;
		}
		super.onDestroy();
	}
}
